define(function (require, exports, module) {
    var $ = require('jquery');
    var jquery = require('jquery');
    require('datepicker');
    var _ = require('underscore');
    var AjaxManager = require('./ajaxManager').AjaxManager;
    var Mustache = require('Mustache');
    var NUM_PAGER_NUM_PER_PAGE = 10;

    function SmRecords () {
        var that = this;

        this.$container = $('#sm-records');
        this.$list = $('#sm-records-list');

        $('#sm-records-from').datepicker({
            dateFormat: 'yy-mm-dd',
            maxDate: 0,
            onClose: function (dateText, inst) {
                var fromDate;
                var toDate;

                if (/^\d{4}\-\d{2}\-\d{2}$/.test(dateText)) {
                    $('#sm-records-to').datepicker('option', 'minDate', new Date(dateText));
                    fromDate = new Date($('#sm-records-from').datepicker('getDate'));
                    toDate = new Date($('#sm-records-to').datepicker('getDate'));

                    if (fromDate > toDate) {
                        $('#sm-records-to').datepicker('setDate', fromDate);
                    }
                } else {
                    $('#sm-records-to').datepicker('option', 'minDate', null);
                }
            }
        });
        $('#sm-records-to').datepicker({
            dateFormat: 'yy-mm-dd',
            maxDate: 0
        });

        this.$form = this.$container.find('form');

        this.$table = this.$container.find('table');

        this.$form.find('.btn').click(function (ev) {
            var form = that.$form.get(0);
            var data = {
                userId: $(document.body).attr('data-userid'),
                fromTime: form.from.value,
                endTime: form.to.value,
                reciever: form.reciever.value,
                status: form.status.value,
                serviceId: form.service.value
            };

            ev.preventDefault();

            SmRecords.queryRecords(data, function (recordArr) {
                that.render(recordArr);
            });
        });

        this.$table.find('tfoot')
            .delegate('.pager-first', 'click', function (ev) {
                ev.preventDefault();

                that.renderPage(1);
            })
            .delegate('.pager-last', 'click', function (ev) {
                ev.preventDefault();

                that.renderPage(that.numberOfPages);
            })
            .delegate('.pager-prev', 'click', function (ev) {
                ev.preventDefault();

                that.renderPage(that.curr - 1);
            })
            .delegate('.pager-next', 'click', function (ev) {
                ev.preventDefault();

                that.renderPage(that.curr + 1);
            });
    }

    SmRecords.prototype.render = function (recordArr) {
        $.each(recordArr, function (index, record) {
            if (record.status > 0) {
                record.status = '发送成功';
            } else {
                record.status = '发送失败';
            }

            record.service = window.SERVICE_MAP[record.serviceId];

            record.buyer = record.userId;
        });


        this.numberOfRecords = recordArr.length;
        this.numberOfPages = Math.ceil(this.numberOfRecords / NUM_PAGER_NUM_PER_PAGE);
        this.recordArr = recordArr;

        if (this.numberOfRecords > 0) {
            this.renderPage(1);


            this.$list.addClass('active');
        } else {
            this.$list.removeClass('active');
        }


    };

    SmRecords.prototype.renderPage = function (number) {
        if (!_.isNumber(number) || number < 1) {
            number = 1;
        }

        this.curr = number;


        this.$table
            .find('tbody')
            .html((Mustache.render($('#sm-records-tpl').html(), {
                records: this.recordArr.splice(NUM_PAGER_NUM_PER_PAGE * (number - 1), NUM_PAGER_NUM_PER_PAGE * (number - 1) + NUM_PAGER_NUM_PER_PAGE - 1)
            })))
            .find('tr').each(function (index) {
                if (index % 2 == 1) {
                    $(this).addClass('even');
                }
            })
            .end()
            .next()
            .find('td')
            .html((Mustache.render($('#pager-tpl').html(), {
                first: (this.numberOfPages > 1 && this.curr != 1),
                last: (this.numberOfPages > 1 && this.curr != this.numberOfPages),
                prev: (this.curr > 1),
                next: (this.curr < this.numberOfPages),
                curr: this.curr,
                total: this.numberOfRecords
            })));

    };

    SmRecords.queryRecords = function (data, onSuccess) {
        /*data.fromTime = "2013-05-01";
        data.endTime = "2013-05-02";
        data.status = "0";
        data.serviceId = "1";*/

        AjaxManager.send('querySendRecord',
            _.extend({}, {
                userId: $(document.body).attr('data-userid')
            }, data)
            , function (result) {
                onSuccess && onSuccess(result.data);
            });
    };

    exports.SmRecords = SmRecords;
});